﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MethodWorx.CMS.Plugins;
using System.Web;

namespace MethodWorx.CMS.Workflow.RedirectTo
{
    public class RedirectToWorkflowComponent : WorkflowComponent<RedirectToController>
    {
        public RedirectToWorkflowComponent(Plugin plugin)
            : base(plugin)
        {

        }

        public static Guid ComponentId = new Guid("{177B21E8-6DFE-4409-A472-FFA4246E8577}");

        public override Guid Id
        {
            get { return ComponentId;  }
        }

        public override string Category
        {
            get { return "Web";  }
        }

        public override string Name
        {
            get { return "Redirect To";  }
        }

        public override string Description
        {
            get { return "Redirects the user to the specified page"; }
        }

        public override StepExecutionResult Execute(IWorkflowExecutionContext context, IWorkflowConfiguration configuration)
        {
            return StepExecutionResult.Finish(() =>
                                                  {
                                                      var current = HttpContext.Current;
                                                      if (current != null)
                                                      {
                                                          current.Response.Redirect(
                                                              this.ParseString(
                                                                  UrlHelper.GenerateContentUrl(((RedirectToConfiguration)configuration).Url, new HttpContextWrapper(current)),
                                                                  context.GetAllWorkflowValues()));
                                                      }
                                                  });
        }
    }
}
